package com.syni.mdd.sjb.community.base.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.community.base.config.QywxProperties;
import com.syni.mdd.sjb.community.base.config.QywxThirdApiProperties;
import com.syni.mdd.sjb.community.base.entity.bo.DepartUserListBo;
import com.syni.mdd.sjb.community.base.entity.bo.DepartmentListBo;
import com.syni.mdd.sjb.community.base.entity.bo.GetRegisterCodeBo;
import com.syni.mdd.sjb.community.base.entity.bo.LoginInfoBo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@EnableConfigurationProperties(QywxThirdApiProperties.class)
@Log4j2
public class QxwxThirdApiUtils {

    @Autowired
    QywxThirdApiProperties qywxThirdApiConfig;

    @Autowired
    QywxProperties qywxProperties;

    public LoginInfoBo getLoginInfo(String providerToken, String authCode) {
        Map<String,String> params = new HashMap<>();
        params.put("auth_code",authCode);
        String postUrl = String.format("%s?access_token=%s",qywxThirdApiConfig.getGet_login_info(),providerToken);
        HttpResponse httpResponse = HttpUtil
                .createPost(postUrl)
                .body(JSON.toJSONString(params))
                .execute();
        log.info("getLoginInfo:{}",httpResponse.body());
        LoginInfoBo loginInfoBo = JSON.parseObject(httpResponse.body(),LoginInfoBo.class);
        return loginInfoBo;
    }

    public DepartmentListBo departmentList(String accessToken) {
        String postUrl = String.format("%s?access_token=%s",qywxThirdApiConfig.getDepartmentlist(),accessToken);
        HttpResponse httpResponse = HttpUtil
                .createGet(postUrl)
                .form(JSON.toJSONString(new HashMap<>()))
                .execute();
        log.info("departmentList：" + httpResponse.body());
        return JSON.parseObject(httpResponse.body(),DepartmentListBo.class);
    }

    public DepartUserListBo departmentUserList(String accessToken,Integer departmentId) {
        String getUrl = String.format("%s?access_token=%s&department_id=%s&fetch_child=1",
                qywxThirdApiConfig.getUserlist(),accessToken,departmentId);
        HttpResponse httpResponse = HttpUtil
                .createGet(getUrl)
                .form(JSON.toJSONString(new HashMap<>()))
                .execute();
        log.info("departmentUserList：" + httpResponse.body());
        return JSON.parseObject(httpResponse.body(), DepartUserListBo.class);
    }

    public GetRegisterCodeBo getRegisterCode(String providerAccessToken, String templateId) {
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("template_id",templateId);
        String getUrl = String.format("%s?provider_access_token=%s",
                qywxThirdApiConfig.getGetRegisterCode(),providerAccessToken);
        HttpResponse httpResponse = HttpUtil
                .createPost(getUrl)
                .body(JSON.toJSONString(paramsMap))
                .execute();
        log.info("getRegisterCode：" + httpResponse.body());
        return JSON.parseObject(httpResponse.body(), GetRegisterCodeBo.class);
    }
}
